package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.domain.TravelComment;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.query.TravelCommentQuery;
import cn.wolfcode.luowowo.repository.TravelCommentRepository;
import cn.wolfcode.luowowo.service.ITravelCommentService;
import cn.wolfcode.luowowo.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class TravelCommentServiceImpl implements ITravelCommentService {
    @Autowired
    private TravelCommentRepository travelCommentRepository;

    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void insert(TravelComment travelComment, String token) {
        travelComment.setId(null);
        UserInfo user = userInfoRedisService.getUserByToken(token);
        travelComment.setUserId(user.getId());
        travelComment.setNickname(user.getNickname());
        travelComment.setHeadImgUrl(user.getHeadImgUrl());
        travelComment.setCity(user.getCity());
        travelComment.setCreateTime(new Date());
        if (travelComment.getType() == 1) {
            TravelComment refComment = travelCommentRepository.findById(travelComment.getRefComment().getId()).get();
            travelComment.setRefComment(refComment);
        }
        travelCommentRepository.save(travelComment);
    }

    @Override
    public void update(TravelComment travelComment) {
        travelCommentRepository.save(travelComment);
    }

    @Override
    public void delete(String id) {
        travelCommentRepository.deleteById(id);
    }

    @Override
    public TravelComment select(String id) {
        return travelCommentRepository.findById(id).get();
    }

    @Override
    public List<TravelComment> listByTravelId(String travelId) {
        return travelCommentRepository.findAllByTravelId(travelId);
    }

    @Override
    public Page<TravelComment> query(TravelCommentQuery qo) {
        Query query = new Query();
        if (StringUtils.hasLength(qo.getKeyword())) {
            Criteria criteria = new Criteria();
            criteria.orOperator(
                    Criteria.where("travelTitle").regex(".*" + qo.getKeyword() + ".*"),
                    Criteria.where("nickname").regex(".*" + qo.getKeyword() + ".*")
            );
            query.addCriteria(criteria);
        }
        PageRequest pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize()
                , Sort.Direction.ASC, "_id");
        return DBHelper.query(mongoTemplate, TravelComment.class, query, pageable);
    }
}
